package space.xxhui.principleof6.utils;

import android.content.Context;
import android.content.SharedPreferences;

import space.xxhui.principleof6.Module.Config;
import space.xxhui.principleof6.interfaces.IImageFactory;

/**
 * Created by hui on 2016/10/21.
 */

public class ParsingConfig {

    public static Config readConfig(Context context) {
        SharedPreferences msg_ = context.getApplicationContext().getSharedPreferences("config", Context.MODE_PRIVATE);
        String cacheType = msg_.getString("CacheType","");
        Config config = new Config();
        config.setCachetype(cacheType);
        return config;
    }

    public static void writeConfig(Config config,Context context) {
        SharedPreferences.Editor editor = context.getApplicationContext().getSharedPreferences("config", Context.MODE_PRIVATE).edit();
        editor.putString("CacheType", config.getCachetype());
        editor.commit();

    }

    //默认是Lrucache
    public static IImageFactory getBean(Context context) {
        String cacheType = readConfig(context).getCachetype();
        if (cacheType.equals(""))
            cacheType = "space.xxhui.principleof6.ImageFactoryImp."+"ImageLruCacheFactory";//注意包名和项目名要加上
        IImageFactory iImageFactory = (IImageFactory) getObject(cacheType);
        return iImageFactory;
    }

    private static Object getObject(String className) {
        try {
            Class c = Class.forName(className);
            Object o = c.newInstance();
            return o;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


}
